package chalmers.edu.jacobla_anerud.MatematiskModelleringModul3;
import java.awt.*;

public abstract class Animation extends java.applet.Applet implements java.lang.Runnable {

    /**
	 * 
	 */
	private static final long serialVersionUID = -452657493780702364L;
	protected Dimension d;  // bitmap size
    protected Image im;  // extra image for drawing
    protected Graphics offscreen; // the offscreen bitmap to draw in
    protected int delay = 100; // in milliseconds
    protected Thread animationThread;

    final public void init() {
    	this.setSize(new Dimension(500,500));
    	this.setMinimumSize(new Dimension(500,500));
    	d = getSize();
    	im = createImage(d.width, d.height);
    	offscreen = im.getGraphics();
    	initAnimator();
    }

    //final public void paint(Graphics g) {update(g);}

    final public void update(Graphics g) {
    	paintAnimator(offscreen);  // first draw offscreen to reduce flicker
    	g.drawImage(im, 0, 0, this);  // then put on screen
    }

    // To be implemented in subclass that does the actual drawing
    protected void initAnimator() {
    	
    } // init for drawing routines
    
    abstract protected void paintAnimator(Graphics g); // the actual drawing will be here

    public void setDelay(int d) {
    	delay=d;
    }

    public void start() {
    	animationThread = new Thread(this);
		animationThread.start();
    }

    public void stop() {
    	animationThread = null;
    }

    @SuppressWarnings("static-access")
	public void run() {
	while (Thread.currentThread() == animationThread) {
	    repaint(); // redraws by somehow calling update
	    try {
	    	Thread.currentThread().sleep(delay);
	    } catch (InterruptedException e) {}
	}
    }
}
